/*
 *   axios二次封装
 */
import { message as antdMessage } from 'ant-design-vue';
import axios from 'axios';

// 创建axios实例
const instance = axios.create({
  //   baseURL: '/dataApi',
  timeout: 3000,
});
// 请求拦截
instance.interceptors.request.use(
  // 在请求发出前作出相应修改，如：添加token、配置请求头等；
  config => {
    const token = localStorage.getItem('token');
    if (token) {
      //  这里面获取的请求头的键(tokenHeader)根据每个后端的习惯封装的名称各不相同
      // config.headers.common['tokenHeader'] as string = token;
    }
    return config;
  },

  // 使用Promise.reject返回请求错误信息
  error => {
    return Promise.reject(error);
  },
);
// 响应拦截
instance.interceptors.response.use(
  // 对响应内容做相应的逻辑处理，如：筛选、过滤等
  response => {
    if (response.request.responseType == 'blob') {
      console.log(response.data);
      return response.data;
    } else {
      const { code, msg } = response.data as { code: number; msg: string };
      let { data } = response.data as { data: any };
      switch (code) {
        case 100201:
          antdMessage.destroy();
          antdMessage.success(msg, 1);
          break;
        case 100202:
          antdMessage.destroy();
          antdMessage.success(msg, 1);
          break;
        case 4004030:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;
        case 100401:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;
        case 100402:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;
        case 100403:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;
        case 100404:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;
        case 400230:
          data = { class: '#87d068', msg };
          antdMessage.destroy();
          antdMessage.success(msg, 1);
          break;
        case 400430:
          // data = { class: '#f50', msg, data: false };
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;
        case 400400:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;

        case 400401:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;
        case 100405:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;
        case 100406:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          break;
        case 100500:
          antdMessage.destroy();
          antdMessage.error(msg, 1);
          return code;
        default:
          break;
      }

      return data;
    }
  },
  error => {
    switch (error?.code) {
      case 'ECONNABORTED':
        antdMessage.destroy();
        antdMessage.error('请求超时', 1);
        break;
    }
    return Promise.reject(error);
  },
);

export function get<T>({ url, params }: { url: string; params?: object }): Promise<T> {
  return instance.get(url, params);
}
export function post<T>({ url, data, config }: { url: string; data?: any; config?: object }): Promise<T> {
  return instance.post(url, data, config);
}
export function put<T>({ url, data, config }: { url: string; data?: any; config?: object }): Promise<T> {
  return instance.put(url, data, config);
}
export function del<T>({ url }: { url: string }): Promise<T> {
  return instance.delete(url);
}

export default { get, post, put, del };
